<template>
  <el-form
    :model="roleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item label="角色名" prop="rolename">
      <el-input v-model="roleForm.roleName"></el-input>
    </el-form-item>

    <el-form-item label="角色描述" prop="roledesc">
      <el-input v-model="roleForm.roleDesc"></el-input>
    </el-form-item>

    <el-form-item label="角色状态" prop="rolestatus">
      <el-input v-model="roleForm.roleStatus"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('roleForm')"
        >立即创建</el-button
      >
      <el-button @click="resetForm('roleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>


<script>
export default {
  data() {
    return {
      roleForm: {
        id: "",
        roleName: "姜总",
        roleDesc: "666",
        roleStatus: "32",
      },
    };
  },
  mounted: function () {
    var app = this;
    if (this.$route.params.id) {
      app.$httpRequest
        .get("role/getById/" + app.$route.params.id, {})
        .then(function ({ data }) {
          console.log("查询id");
          console.log(data);
          app.roleForm = data.data;
        })
        .catch(function (error) {
          //失败后调用
        })
        .then(function () {
          //不论成功或者失败后都调用
        });
    }
  },
  methods: {
    submitForm(formName) {
      console.log(this.$route.params.id);
      if (this.$route.params.id) {
        this.roleForm.id = this.$route.params.id;
        
        var app = this;
        this.$httpRequest
          .post("role/update", this.roleForm)
          .then(function ({ data }) {
            if (data.code ==0 ) {
              console.log("修改成功");
              app.$message({
                showClose: true,
                message: "恭喜你,修改成功",
                type: "success",
              });
              app.$router.push("/toRoleList");
            }
          })
          .catch(function (error) {
            //失败后调用
          })
          .then(function () {
            //不论成功或者失败后都调用
          });
      } else {
        var app = this;
        this.$httpRequest
          .post("role/save", this.roleForm)
          .then(function ({ data }) {
            if (data.code == 0) {
              app.$message({
                showClose: true,
                message: "恭喜你，添加成功",
                type: "success",
              });
              console.log("添加成功");
              app.$router.push("/toRoleList");
            }
          })
          .catch(function (error) {
            //失败后调用
          })
          .then(function () {
            //不论成功或者失败后都调用
          });
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    toIndex() {
      console.log("进来了");
      this.$router.push("/toRoleList");
    },
  },
};
</script>

<style>
</style>